<template>
	<el-dialog title="新增文件夹" append-to-body :visible.sync="visible" width="560px">
		<el-form size="medium" ref="form" :model="formData" :rules="formRules" label-width="100px" v-loading="loading" class="folder_form_wrapper">
			<el-form-item label="文件夹名称" prop="oldName">
				<el-input v-model="formData.oldName" placeholder="文件夹名称"></el-input>
			</el-form-item>
			<el-form-item label="文件夹描述" prop="content">
				<el-input type="textarea" placeholder="文件夹名称" :autosize="{ minRows: 3, maxRows: 6 }" v-model="formData.content"></el-input>
			</el-form-item>
			<el-form-item label="排序" prop="sort">
				<el-input-number v-model="formData.sort" controls-position="right" :min="1"></el-input-number>
			</el-form-item>
		</el-form>
		<template slot="footer">
			<el-button size="medium" @click="visible = false">取消</el-button>
			<el-button size="medium" type="primary" :loading="btnLoading" @click="handleSubmitForm">确定</el-button>
		</template>
	</el-dialog>
</template>

<script>
import { createFolder, getResourceList } from '@/api/data/tbNetDisk';
export default {
	props: {
		value: Boolean
	},
	data() {
		const validFolderName = (rule, value, callback) => {
			if (!value) {
				callback(new Error('请输入文件夹名称'));
			} else if (this.libFolder.some((item) => item.oldName === value)) {
				callback(new Error('已存在名称为【' + value + '】的文件夹'));
			} else {
				callback();
			}
		};
		return {
			loading: false,
			visible: this.value,
			libFolder: [],
			formData: {},
			formRules: {
				oldName: [{ required: true, trigger: 'blur', validator: validFolderName }]
			},
			btnLoading: false
		};
	},
	computed: {
		libraryId() {
			return this.$route.params.id;
		},
		checkedFolder() {
			return this.$route.query.folder || '0';
		}
	},
	watch: {
		value(flag) {
			this.visible = flag;
		},
		async visible(flag) {
			this.$emit('input', flag);
			if (!flag) return false;
			this.loading = true;
			await this.getLibFolderData();
			this.loading = false;
			this.$nextTick(() => {
				this.$refs['form'].clearValidate();
				this.formData = {};
			});
		}
	},
	methods: {
		//获取资源库文件夹数据
		async getLibFolderData() {
			let params = { page: 1, limit: 99999, isFolder: '1', dirId: this.libraryId, pId: this.checkedFolder };
			let result = await getResourceList(params).then((res) => res.data);
			if (result.code === '0') {
				this.libFolder = result.data || [];
			}
		},

		handleSubmitForm() {
			this.$refs['form'].validate(async (valid) => {
				if (!valid) return false;
				this.btnLoading = true;
				await this.createFolderRequest();
				this.btnLoading = false;
			});
		},

		//创建文件夹请求
		async createFolderRequest() {
			let params = { ...this.formData, dirId: this.libraryId, systemKey: '1', pid: this.checkedFolder };
			let result = await createFolder(params).then((res) => res.data);
			if (result.code === '0') {
				this.visible = false;
				this.$message.success('创建文件夹成功');
				this.$store.dispatch('getCurLibFolder', this.libraryId);
				this.$emit('refresh');
			} else {
				this.$message.error('创建文件夹成功');
			}
		}
	}
};
</script>

<style lang="scss" scoped>
.folder_form_wrapper {
	width: calc(100% - 10px);
	padding: 10px 10px 0 0;
}
</style>
